package com.eco.response;

import java.text.MessageFormat;

public class ResultTool {
    public ResultTool() {
    }

    public static <R extends BaseResponse> R toResponse(R r) {
        return r;
    }

    public static <T> BaseResponse success() {
        return BaseResponse.instance();
    }

    public static <T> BaseResponse successNoData(String message) {
        BaseResponse response = BaseResponse.instance();
        response.setMsg(message);
        return response;
    }

    public static <T> BaseResponse successNoData() {
        return success();
    }

    public static <T> BaseResponse successNoData(String message, Object... args) {
        BaseResponse response = BaseResponse.instance();
        response.setMsg(MessageFormat.format(message, args));
        return response;
    }

    public static <T> BaseResponse<T> success(T t) {
        BaseResponse<T> response = BaseResponse.instance();
        response.setData(t);
        return response;
    }

    public static <T> BaseResponse success(T t, String message, Object... args) {
        BaseResponse response = BaseResponse.instance();
        response.setData(t);
        response.setMsg(MessageFormat.format(message, args));
        return response;
    }

    public static <T> BaseResponse success(ResponseCodeEnum message, Object... args) {
        BaseResponse response = BaseResponse.instance();
        response.setMsg(MessageFormat.format(message.getMessage(), args));
        response.setMsgCode(message.getCode());
        return response;
    }

    public static <T> BaseResponse success(T t, ResponseCodeEnum message, Object... args) {
        BaseResponse response = BaseResponse.instance();
        response.setMsg(MessageFormat.format(message.getMessage(), args));
        response.setMsgCode(message.getCode());
        response.setData(t);
        return response;
    }

    public static <T> PageResponse success(T t, long total) {
        PageResponse<T> response = new PageResponse();
        response.setData(t);
        response.setTotal(total);
        return response;
    }

    public static <T> PageResponse success(T t, long total, String message, Object... args) {
        PageResponse<T> response = new PageResponse();
        response.setData(t);
        response.setMsg(MessageFormat.format(message, args));
        response.setTotal(total);
        return response;
    }

    public static <T> PageResponse success(T t, long total, ResponseCodeEnum message, Object... args) {
        PageResponse<T> response = new PageResponse();
        response.setMsg(MessageFormat.format(message.getMessage(), args));
        response.setMsgCode(message.getCode());
        response.setData(t);
        response.setTotal(total);
        return response;
    }

    public static <T> BaseResponse fail() {
        BaseResponse response = BaseResponse.instanceErr();
        return response;
    }

    public static <T> BaseResponse fail(String message) {
        BaseResponse response = BaseResponse.instanceErr();
        response.setMsg(message);
        return response;
    }
}